import '../commons/common.js'
import './booking.less'

import '../commons/MyRouter.js'
import checkerUtils from '../commons/js/check'

var __g = {
    //hasSelectRoom:false
}

$('body').on('click','.js-booking-btn',function(){
    var curPage = $('#page-main'),
        nextPage = $('#page-order')
    //curPage.removeClass('page-current')
    //nextPage.addClass('page-current')
    var $roomCard= $(this).closest('.js-room-card')
    var code = $roomCard.attr('data-type-code'),
        name = $roomCard.attr('data-type-name'),
        price = $roomCard.attr('data-price'),
        maxRoomCount = $roomCard.attr('data-room-count')
    $('#order-room-type').attr('data-type-code',code).text(name)
    $('#order-money').text(price).attr('data-price',price)
    $('#order-room-count').attr('data-max',maxRoomCount)
    $.myRouter.forward(nextPage,curPage)
})

var $inputRoomCount = $('#order-room-count');

function calcMoney(){
    var roomCount = $inputRoomCount.val()*1
    var $orderMoney = $('#order-money');
    var price  = $orderMoney.attr('data-price')*1;
    $orderMoney.text((price* roomCount).toFixed(1))
}
$inputRoomCount.on('input',function(){
    calcMoney()
})
$('#room-up').click(function(){
    var max = $inputRoomCount.attr('data-max')*1;
    var roomCount = $inputRoomCount.val()*1
    if(roomCount>=max){
        return $.alert('此种房型,没有更多的房间了')
    }
    $inputRoomCount.val(roomCount+1)
    calcMoney()
})

$('#room-down').click(function(){
    //var max = $inputRoomCount.attr('data-max')*1;
    var roomCount = $inputRoomCount.val()*1
    if(roomCount<=1){
        return $.alert('至少预订一间')
    }
    $inputRoomCount.val(roomCount-1)
    calcMoney()
})

function getYMD(d){
    var month = d.getMonth()+1,
        day = d.getDate()
    return{
        year:d.getFullYear(),
        month:month<10?'0'+month:month,
        day:day<10?'0'+day:day
    }
}
function getYMDStr(d){
    var d = getYMD(d)
    return `${d.year}-${d.month}-${d.day}`
}

$('#submit-order').click(function(){
    var data={
        hotelno : __hn,
        rtCode : $('#order-room-type').attr('data-type-code'),
        phoneNum:$('#input-phone').val(),
        vcode : $('#input-yzm').val(),
        linkMan: $('#input-username').val(),
        remarks : $('#input-remark').val(),
        priceSum : $('#order-money').text(),
        beginDate : $('#start-date').text(),
        endDate:$('#end-date').text(),
        rmCount : $inputRoomCount.val()
    }

    if(!data.linkMan){
        return $.alert('请填写联系人信息')
    }
    if(!checkerUtils.isYzmValid(data.vcode)){
        return $.alert('验证码格式错误,为4-6为数字')
    }
    if(!checkerUtils.isPhoneValid(data.phoneNum)){
        return $.alert('手机号格式错误')
    }


    $.showPreloader('订单提交中,请稍后')
    $.ajax({
        type:'post',
        url:'/submit-order',
        data:data,
        success:function(e){
            if(e.error){
                return $.alert(e.error +'.原因:'+e.reason)
            }


            $('.js-s-order-num').text(e.orderNum)
            $('.js-s-room-type').text($('#order-room-type').text())
            $('.js-s-in-time').text(data.beginDate)
            $('.js-s-order-time').text(e.orderDate)
            $('.js-s-link-man').text(data.linkMan)
            $('.js-s-phone').text(data.phoneNum)



            if(e.info){
                $.alert(e.info);
            }

            //var $selectRoom = $('.js-select-room');
            //var href = $selectRoom.attr('href')
            //$selectRoom.attr('href',href+'&rtCode='+data.rtCode+'&orderNum='+e.orderNum)
            var curPage = $('#page-order'),
                nextPage = $('#page-order-success');
            $.myRouter.forward(nextPage,curPage)
        },
        error:function(){
            $.alert('sorry!发生错误,请稍后重试')
        },
        complete:function(){
            $.hidePreloader()
        }
    })

})

$('.js-cancel-order').click(function(){
    var orderNum = $('.js-s-order-num').text(),
        hotelno = __hn;

    $.showPreloader('订单取消中,请稍后')

    $.ajax({
        type:'post',
        url:'/cancel-order',
        data:{
            orderNum:orderNum,
            hotelno:hotelno
        },
        success:function(e){
            if(e.error){
                return $.alert(e.error +'.原因:'+e.reason)
            }
            var curPage = $('#page-order-success'),
                nextPage = $('#page-main');
            $.myRouter.back(nextPage,curPage)
        },
        error:function(){
            $.alert('sorry!发生错误,请稍后重试')
        },
        complete:function(){
            $.hidePreloader()
        }
    })
})

//选房,我的订单里也有,先复制code,完成业务
$('.js-select-room').click(function(){
    //if(__g.hasSelectRoom){
    //    return $.alert('您已经选过房了')
    //}
    var orderNum = $('.js-s-order-num').text(),
        hotelno = __hn;
    var roomCount = $inputRoomCount.val()*1
    $('#select-room-list').attr('data-room-count',roomCount)
    $('#select-room-count').text(`您可以选择${roomCount}间房`);

    $.showPreloader('正在请求房间列表,请稍后')
    $.ajax({
        type:'post',
        url:'/select-room',
        data:{
            orderNum:orderNum,
            hotelno:hotelno,
            rtCode:$('#order-room-type').attr('data-type-code')
            //orderNum:'RS1512070009003',
            //hotelno:hotelno,
            //rtCode:'BR'
        },
        success(e){
            var roomList = e.roomList
            var html = ''
            for(var i in roomList){
                var room = roomList[i];
                html+=`<div class="room-wrapper">
                    <div class="room-number">${room.rmNum}</div>
                </div>`
            }
            $('#select-room-list').html(html)
            var curPage = $('#page-order-success'),
                nextPage = $('#page-select-room');
            $.myRouter.forward(nextPage,curPage)
        },
        error:function(){
            $.alert('sorry!发生错误,请稍后重试')
        },
        complete:function(){
            $.hidePreloader()
        }
    })
})
//$('.js-select-room').trigger('click')

$('#select-room-list').on('click','.room-wrapper',function(){
    $(this).toggleClass('active')
})



$('#submit-select-room').click(function(){
    var selectList = []
    $('.room-wrapper.active').each(function(){
        selectList.push($(this).find('.room-number').text())
    })
    if(selectList.length==0){
        return $.alert('您还没有选择房间')
    }
    //var maxCount = $inputRoomCount.val()*1;
    var maxCount = $('#select-room-list').attr('data-room-count')*1;

    if(selectList.length>maxCount){
        return $.alert(`您预订了${maxCount}间房,选房数不能超过.`)
    }
    var orderNum = $('.js-s-order-num').text(),
        hotelno = __hn;

    $.showPreloader('选房提交中,请稍后')

    $.ajax({
        type:'post',
        url:'/submit-room',
        data:{
            orderNum:orderNum,//'RS1512070009003',
            hotelno:hotelno,
            rooms:selectList.join(',')
        },
        success(e){
            if(e.error){
                return $.alert(e.error +'.原因:'+e.reason)
            }
            //__g.hasSelectRoom = true
            var nextPage = $('#page-order-success'),
                curPage = $('#page-select-room');
            $.myRouter.back(nextPage,curPage)
            $.alert(`选房成功,请在${e.times}分钟内进行付款`);
        },
        error:function(){
            $.alert('sorry!发生错误,请稍后重试')
        },
        complete:function(){
            $.hidePreloader()
        }

    })
})